// pages/write-off/order-detail.js
const format = require('../../utils/format.js')
const numeral = require("../../vendor/numeral/numeral.min")
const app = getApp()
const statusObj = {
    "0": "待付款",
    "12": "已支付",
    "100": "已完成",
}

Page({

    data: {},

    onLoad: function(options) {
        this.setData(options)
        this.setData({
            statusObj,
        })
        this.loadData()
    },
    loadData() {
        wx.showLoading({
            title: '加载中...',
        })
        app.http.get('hx-order/info', {
            data: {
                order_id: this.data.order_id
            }
        }).then(res => {
            let t = new Date(parseInt(res.data.created_at) * 1000);
            res.data.payed = numeral(res.data.payed / 100).format("0,0.00")
            res.data.total_amount = numeral(res.data.total_amount / 100).format("0,0.00")
            res.data.created_at = format.dateformat.format(t)
            this.setData(res.data)
            wx.hideLoading()
        })
    },
    /**
     * 重新支付
     */
    payAgain(evt) {
        let order_id = this.data.order_id
        if (this.data.paying) return
        this.setData({
            paying: true
        })
        wx.showLoading({
            title: '请稍等',
        })
        app.http.post('pay/order', {
            data: {
                order_id
            }
        }).then(res => {
            wx.hideLoading()
            let jsPay = res.data.js
            if (res.data.pay_type == 2) {
                wx.showToast({
                    icon: 'none',
                    title: '支付成功',
                })
                this.onPullDownRefresh()
            }
            if (jsPay) {
                const obj = Object.assign(jsPay, {
                    success: res => {
                        wx.showToast({
                            icon: 'none',
                            title: '支付成功',
                        })
                        this.loadData()
                    },
                    fail: err => {
                        wx.showToast({
                            icon: 'none',
                            title: '支付取消',
                        })
                    }
                });
                wx.requestPayment(obj)
            }
            this.setData({
                paying: false
            })
        }).catch(err => {
            console.log(err.message)
            app.wxToast.error(err.message)
            this.setData({
                paying: false
            })
        })
    },

})